package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.LineLevelDao;
import com.sailfish.springbootdemo.dao.db1.LineTechBigTypeDao;
import com.sailfish.springbootdemo.dao.db1.LineTechSmallTypeDao;
import com.sailfish.springbootdemo.dao.db1.TechCapacityRuleDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.LineTechBigType;
import com.sailfish.springbootdemo.pojo.db1.LineTechSmallType;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LineTechBigTypeServiceImpl implements LineTechBigTypeService {

    @Autowired
    private LineTechBigTypeDao lineTechBigTypeDao;

    @Autowired
    private LineTechSmallTypeDao lineTechSmallTypeDao;

    @Autowired
    private LineLevelDao lineLevelDao;

    @Autowired
    private TechCapacityRuleDao techCapacityRuleDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "工艺整线线体";

    @Override
    public List<LineTechBigType> getAllLineTechBigType(String searchValue) {
        List<LineTechBigType> list = lineTechBigTypeDao.getAllLineTechBigType(searchValue);
        for (LineTechBigType lineTechBigType : list) {
            String lineLevelIds = lineTechBigType.getLineLevelIds();
            if (lineLevelIds != null && !"".equals(lineLevelIds)) {
                List<Integer> idsList = new ArrayList<>();
                String[] idArr = lineLevelIds.split(",");
                for (String s : idArr) {
                    idsList.add(Integer.parseInt(s));
                }
                lineTechBigType.setLineLevelList(lineLevelDao.getLineLevelByIds(idsList));
            }
            List<LineTechSmallType> allLineTechSmallTypeByBigType = lineTechSmallTypeDao.getAllLineTechSmallTypeByBigType("", lineTechBigType.getLineTechBigTypeId());
            for (LineTechSmallType lineTechSmallType : allLineTechSmallTypeByBigType) {
                lineTechSmallType.setTechCapacityRuleList(techCapacityRuleDao.getAllTechCapacityRule(null, lineTechBigType.getLineTechBigTypeId(), lineTechSmallType.getLineTechSmallTypeId()));
            }
            lineTechBigType.setLineTechSmallTypeList(allLineTechSmallTypeByBigType);
        }
        return list;
    }

    @Override
    public Result addOrUpdateLineTechBigType(LineTechBigType lineTechBigType, String mode) {
        try {
            if (lineTechBigType == null) {
                return ResultUtil.error(500, "缺少参数lineTechBigType", "Miss Param lineTechBigType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (lineTechBigType.getLineTechBigTypeId() == null || "".equals(lineTechBigType.getLineTechBigTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数lineTechBigTypeId", "Miss Param lineTechBigTypeId", null);
                }

                List<LineTechBigType> lineTechBigTypeList = lineTechBigTypeDao.judgeUniqueLineTechBigType(lineTechBigType.getLineTechBigTypeId(), lineTechBigType.getLineTechBigTypeName());
                if (lineTechBigTypeList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                LineTechBigType oldLineTechBigType = lineTechBigTypeDao.getLineTechBigTypeById(lineTechBigType.getLineTechBigTypeId());

                lineTechBigType.setUpdateTime(new Date());
                Integer integer = lineTechBigTypeDao.updateLineTechBigType(lineTechBigType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldLineTechBigType), JSONObject.toJSONString(lineTechBigType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<LineTechBigType> lineTechBigTypeByName = lineTechBigTypeDao.getLineTechBigTypeByName(lineTechBigType.getLineTechBigTypeName());
                if (lineTechBigTypeByName.size() > 0) {
                    return ResultUtil.error(500, "该项已存在", "Already Exist", null);
                }

                lineTechBigType.setCreateTime(new Date());
                Integer integer = lineTechBigTypeDao.addLineTechBigType(lineTechBigType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(lineTechBigType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result deleteLineTechBigType(Integer lineTechBigTypeId) {
        try {
            if (lineTechBigTypeId == null || "".equals(lineTechBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数lineTechBigTypeId", "Miss Param lineTechBigTypeId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            LineTechBigType oldLineTechBigType = lineTechBigTypeDao.getLineTechBigTypeById(lineTechBigTypeId);

            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteLineTechBigType(lineTechBigTypeId);
            if (stringIntegerMap.get("lineTechSmallTypeNum") > 0 || stringIntegerMap.get("techCapacityRuleNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("lineTechSmallTypeNum") + "处关联项目小类，" + stringIntegerMap.get("techCapacityRuleNum") + "处关联工艺整线能力规则",
                        "Forbidden Delete", null);
            }
            Integer integer = lineTechBigTypeDao.deleteLineTechBigType(lineTechBigTypeId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldLineTechBigType), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
